import 'dart:async';

import 'package:flutter/services.dart';

final eventChannel = const EventChannel("CHANNEL_EVENT");
StreamSubscription<dynamic>? streamSubscription;

/// 设置监听
/// PS: 不能在初始化 channel 时通过 .. 方式设置 会不生效
void initEventChannel() {
  streamSubscription = eventChannel.receiveBroadcastStream(["event"]).listen((dynamic event) {
    // 处理事件
    print("event: $event");
  }, onError: (error, StackTrace stack) {
    // 异常
  }, onDone: () {
    // 结束
  });
}

void stopListen() {
  streamSubscription?.cancel();
  streamSubscription = null;
}
